﻿#region NeoPatterns
// 
// NeoPatterns framework is released under LGPLv3 license.
// See http://www.opensource.org/licenses/lgpl-3.0.html for more details.
// 
// Please visit http://neopatterns.wikidot.com for more information about NeoPatterns
// 
// Copyright (c) Pascal Craponne 2008
// 
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace NeoPatterns.Core.Aop.Implementation
{
    /// <summary>
    /// Exposes a method to start advisors chain call
    /// This class is public, since call from generated assembly. 
    /// Semantically, it is internal.
    /// </summary>
    public static class AspectInvocation
    {
        /// <summary>
        /// Stores overriden method infos and original methods as a pair.
        /// </summary>
        public static IDictionary<MethodInfo, MethodInfo> OriginalMethods = new Dictionary<MethodInfo, MethodInfo>();

        /// <summary>
        /// Runs an interception.
        /// We use a static method here, if one day we want to reuse Invocations or change mecanism,
        ///  it will be easier from C# code
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <param name="crossCuttings"></param>
        /// <param name="targetMethod"></param>
        /// <param name="baseMethod"></param>
        public static object Process(object target, object[] parameters, IEnumerable crossCuttings,
                                     MethodInfo targetMethod, Action<IInvocation> baseMethod)
        {
            var originalMethod = OriginalMethods[targetMethod];
            var invocation = new Invocation(target, parameters, crossCuttings, originalMethod, baseMethod);
            invocation.Proceed();
            return invocation.ReturnValue;
        }
    }
}